<?php
namespace controllers;

/**
 * 导入数据助手 控制器 Trait
 * 
 * @desc    地区控制器
 * @version 1.0.0 
 * @author  Jason
 * @date    2017年5月2日 下午5:09:07
 */
trait ImportData
{
	// RAW一列，含表头，语言为中文
	public function create_country()
	{
// 		$column = $this->input->post('first_column');
// 		$column = $this->input->input_stream();
		$column = $this->input->raw_input_stream;
// 		$rows = explode("\n", $column);
		$rows = explode("\r", $column);
		$thead = array_shift($rows);
		$language = $this->chinese_to_english($thead);
		
		if ($language !== 'chinese') {
			die("请导入中文的！");
		}
		
		$parent_id = 0;
		$sort_order = 0;
		$count = 0;
		foreach ($rows as $name) {
			$sort_order += 1;
			$data = [
					'name' => $name,
					'parent_id' => $parent_id,
					'sort_order' => $sort_order,
			];
			list($status, $instance) = $this->mod_area->create_instance($data);
			$status and $count++;
		}
		
		echo "成功导入{$count}条国家信息！";
		log_message('error', sprintf('Import "%d" countries successfuly!', $count));
	}
	
	// RAW多列，含表头，第一列须为ID数据
	public function create_country_language()
	{
		$table = $this->input->raw_input_stream;
		$rows = explode("\r", $table);
		
		print_R($rows);
		$metrix = [];
		$thead = array_shift($rows);
		$thead = explode("\t", $thead);
		array_shift($thead);
		
		foreach ($rows as $row) {
			$array = explode("\t", $row);
			$resource = array_shift($array);
			foreach ($thead as $key => $value) {
				$language = $this->chinese_to_english($value);
				$name = $array[$key];
				
				$metrix[] = [
						'resource' => $resource,
						'language' => $this->chinese_to_english($thead[$key]),
						'name' => $array[$key],
						'short_name' => $array[$key],
				];
			}
		}
// 		print_r($metrix);
		
		foreach ($metrix as $data)
		{
			// 添加语言
			$resource = $data['resource'];
			$module = [$this->mod_area, 'master'];
			$language = $data['language'];
			
			$this->lib_language->create_language($resource, $data, $module, $language);
		}
	}
	
	// RAW多列，含表头，第一列须为PARENT_ID数据，第二列为城市名称，语言为中文
	public function create_city()
	{
		$table = $this->input->raw_input_stream;
		$rows = explode("\r", $table);
		
		print_R($rows);
		$metrix = [];
		$thead = array_shift($rows);
		
		list($parent_id, $language) = explode("\t", $thead);
		$language = $this->chinese_to_english($language);
		
		if ($parent_id !== 'parent_id'|| $language !== 'chinese') {
			die("表头首列为'parent_id' & 请导入中文的！");
		}
		
		$parent_id = 0;
		$sort_order = 0;
		$count = 0;
		foreach ($rows as $row) {
			list($parent_id, $name) = explode("\t", $row);
			$sort_order += 1;
			$data = [
					'name' => $name,
					'parent_id' => $parent_id,
					'sort_order' => $sort_order,
			];
			list($status, $instance) = $this->mod_area->create_instance($data);
			$status and $count++;
			if(!$status){
				echo sprintf("导入城市 '%s' 时发生错误！", var_export($data, true));
				log_message('error', sprintf('Error occurred on import city data: "%s"!', var_export($data, true)));
			}
		}
		
		echo "成功导入{$count}条城市信息！";
		log_message('error', sprintf('Import "%d" cities successfuly!', $count));
	}
	
	// RAW多列，含表头，第一列须为PARENT_ID数据
	public function create_city_language()
	{
		$this->create_country_language();
	}
	
	private function chinese_to_english($language)
	{
		// $language:
		// 国家(中文翻译)	英文翻译	法语翻译	德语翻译	西班牙语翻译	意大利语翻译
		// 城市(英文)	城市(中文)	城市(法语)	城市(德语)	城市(西班牙语)	城市(意大利语)
		
		$mapper = [
				'english' => '英',
				'chinese' => '中',
				'french' => '法',
				'german' => '德',
				'spanish' => '西班牙',
				'italian' => '意大利',
		];
		
		foreach ($mapper as $english => $chinese) {
			if (FALSE !== strpos($language, $chinese)) {
				return $english;
			}
		}
		
		$default = 'english';
		log_message('error', sprintf('Language "%s" was parsed to "%s" by default!', $language, $default));
		
		return $default;
	}
}